import Locale from "@/store/Locale";
import Config from "@/config/Config";

class Global {
    get token(): LoginResponse {
        return {
            token: this.isAutoLogin ? localStorage.getItem(this.Token) : sessionStorage.getItem(this.Token),
            refreshToken: this.isAutoLogin ? localStorage.getItem(this.RefreshToken) : sessionStorage.getItem(this.RefreshToken)
        };
    }

    //是否自动登录
    get isAutoLogin(): boolean {
        return localStorage.getItem(this.AutoLogin) === "1";
    }

    get Token(): string {
        return "thingsboard_token";
    }

    get RefreshToken(): string {
        return "thingsboard_refreshToken";
    }

    get AutoLogin(): string {
        return "AutoLogin";
    }

    get baseURL(): string {
        return this._baseURL;
    }
    get wsURL(){
        return this._wsURL;
    }

    set baseURL(baseURL: string) {
        this._baseURL = baseURL;
    }

    get locale(): Locale {
        return this._locale;
    }

    private _baseURL = import.meta.env.MODE === "production" ?
        Config.PRODUCTION_IP_CONFIG.API_URL : Config.DEVELOPMENT_IP_CONFIG.API_URL;
    private _wsURL = import.meta.env.MODE === "development" ?
        Config.PRODUCTION_IP_CONFIG.WS_URL : Config.DEVELOPMENT_IP_CONFIG.WS_URL;
    private _locale: Locale = new Locale();

    // private readonly _isAutoLogin: string | boolean = localStorage.getItem(this.AutoLogin);

    constructor() {
    }

    //设置语言
    setLanguage(language: Language) {
        this._locale.language = language;
    }

    //设置是否自动登录
    setAutoLogin(autoLogin: boolean) {
        if (autoLogin) {
            localStorage.setItem(this.AutoLogin, "1");
            sessionStorage.setItem(this.AutoLogin, "1");
        } else {
            localStorage.removeItem(this.AutoLogin)
            localStorage.removeItem(this.Token)
            localStorage.removeItem(this.RefreshToken)
        }
    }
}

export enum Language {
    English,
    Chinese
}

const global: Global = new Global();
export default global;
